package net.lovesrc.support.service.mobileverifycode;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.Requests;
import com.zhouzhou.fastjava.util.StringKit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DuanZuMobileVerifyCodeService extends AbstractMobileVerifyCodeService implements MobileVerifyCodeService {

    private static final Logger logger = LoggerFactory.getLogger(DuanZuMobileVerifyCodeService.class);

    public static final String DEFAULT_HOST = "http://api.jmyzm.com";

    private String username;

    private String password;

    private String gatewayUrl;

    public DuanZuMobileVerifyCodeService(String host, String username, String password) {
        this.username = username;
        this.password = password;
        this.gatewayUrl = host + "/http.do";
    }

    @Override
    protected String doGetVerifyCodeFromPhone(String itemId, String phone) {
        JSONObject params = new JSONObject();
        params.put("action", "getVcodeAndReleaseMobile");
        params.put("token", getToken());
        params.put("mobile", phone);
        params.put("pid", itemId);
        params.put("uid", username);
        params.put("author_uid", "liuzheqiang511");
        String body = Requests.get(gatewayUrl)
                .params(params)
                .execute()
                .asText();
        if (body.startsWith(phone + "|")) {
            return StringUtils.substringAfter(body, phone + "|");
        }
        return null;
    }

    @Override
    public void addBlack(String itemId, String phone) {
        logger.info("Add Black Phone: {}", phone);
        JSONObject params = new JSONObject();
        params.put("action", "addIgnoreList");
        params.put("token", getToken());
        params.put("mobiles", phone);
        params.put("pid", itemId);
        params.put("uid", username);
        Requests.get(gatewayUrl)
                .params(params)
                .execute()
                .asText();
    }

    @Override
    public void releasePhone(String itemId, String phone) {
        // 自动释放
    }

    @Override
    public String getPhone(String itemId, String phone) {
       return getPhone(itemId,phone,null);
    }

    @Override
    public String getPhone(String itemId, String phone, String card) {

        JSONObject params = new JSONObject();
        params.put("action", "getMobilenum");
        params.put("pid", itemId);
        params.put("mobile", phone);
        params.put("token", getToken());
        params.put("uid", username);
        String body = Requests.post(gatewayUrl)
                .params(params)
                .execute()
                .asText();
        if (body.endsWith("|" + getToken())) {
            return StringKit.extract(body, "(\\d{11})");
        } else {
            throw BizException.messageOf("获取手机号失败");
        }
    }

    @Override
    protected String login() {
        JSONObject params = new JSONObject();
        params.put("action", "loginIn");
        params.put("uid", username);
        params.put("pwd", password);
        String body = Requests.post(gatewayUrl)
                .params(params)
                .execute()
                .asText();
        if (body.startsWith(username + "|")) {
            return StringUtils.substringAfter(body, username + "|");
        } else {
            throw BizException.messageOf("登录失败");
        }
    }

}
